exports.auth = function(req, res, next) {
  return function(req, res, next) {
    if (req.isAuthenticated && req.isAuthenticated()) {
      res.locals.isAuthenticated = true;
    } else {
      res.locals.isAuthenticated = false;
    }
    next();
  };
};

exports.ensureLoggedIn = function(req, res, next) {
  return function(req, res, next) {
    if (req.isAuthenticated && req.isAuthenticated()) {
      req.returnTo = req.header("RentoTo") || "/editor";
      next();
    } else {
      if (req.header("X-Requested-With")) {
        res.json({
          code: -2,
          goto: "/login"
        });
      } else {
        res.redirect("/login");
      }
    }
  };
};
